package com.cytech.cloudclinic.moudle.main.patient;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.ClinicAdapter;
import com.cytech.cloudclinic.adapter.PatientAdapter;
import com.cytech.cloudclinic.base.BaseFragment;
import com.cytech.cloudclinic.bean.Patient;
import com.cytech.cloudclinic.moudle.main.patient.detail.PatientDetailActivity;
import com.yechaoa.yutils.SpUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.cytech.cloudclinic.app.Constant.USER_TOKEN;

public class PatientFragment extends BaseFragment<PatientPresenter> implements PatientView {

    @BindView(R.id.rv_patient_main)
    RecyclerView recyclerView;
    @BindView(R.id.tv_add_patient_phone)
    TextView phoneTv;

    private List<Patient> mList;
    private PatientAdapter mAdapater;

    public static PatientFragment newInstance() {
        PatientFragment fragment = new PatientFragment();
        //如果有参数，使用Bundle传递
        return fragment;
    }


    @Override
    protected PatientPresenter createPresenter() {
        return new PatientPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_patient;
    }

    @Override
    protected void initView() {
        phoneTv.setText(SpUtil.getString(SpUtil.getString(USER_TOKEN)));
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mAdapater = new PatientAdapter(mList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(mAdapater);

        presenter.getPatientList();

        mAdapater.addChildClickViewIds(R.id.item_patient_detail_btn);
        mAdapater.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Patient patient = mAdapater.getItem(position);
                PatientDetailActivity.actionStart(getActivity(),patient.patient_id,patient.name);
            }
        });
    }

    @Override
    public void getPatientListSuccess(List<Patient> data) {
        mAdapater.setNewInstance(data);
    }
}
